package com.springboot.cloud.sysadmin.organization.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.sysadmin.organization.dao.DictTypeMapper;
import com.springboot.cloud.sysadmin.organization.entity.param.DictTypeQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.DictType;
import com.springboot.cloud.sysadmin.organization.service.IDictTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */
@Service
@Slf4j
public class DictTypeService extends ServiceImpl<DictTypeMapper, DictType> implements IDictTypeService {

    @Override
    public boolean add(DictType dictType) {
        DictType dictType1 = this.getOne(new QueryWrapper<DictType>()
                .eq(StrUtil.isNotBlank(dictType.getDictName()), "dict_name", dictType.getDictName()), false
        );
        log.debug("dictType1:", dictType1);
        if (ObjectUtil.isNull(dictType1)) {
            return this.saveOrUpdate(dictType);
        }
        return false;
    }

    @Override
    public boolean delete(Set<String> ids) {

        return this.removeByIds(ids);
    }

    @Override
    public boolean update(DictType dictType) {
        DictType dictType1 = this.getById(dictType.getId());
        if (ObjectUtil.isNull(dictType1) || dictType1.getId().equals(dictType.getId()) && get(dictType.getId())) {
            return false;
        }
        return this.saveOrUpdate(dictType);
    }

    @Override
    public DictType getDictType(String id) {
        return this.getById(id);
    }

    @Override
    public boolean get(String id) {
        DictType dictType = this.getById(id);
        if (ObjectUtil.isNull(dictType)) {
            return false;
        }
        return true;
    }

    @Override
    public IPage<DictType> query(Page page, DictTypeQueryParam dictTypeQueryParam) {
        SimpleDateFormat convert = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<DictType> queryWrapper = dictTypeQueryParam.build();
        queryWrapper.like(StrUtil.isNotBlank(dictTypeQueryParam.getDictName()), "dict_name", dictTypeQueryParam.getDictName());
        queryWrapper.like(StrUtil.isNotBlank(dictTypeQueryParam.getDictType()), "dict_type", dictTypeQueryParam.getDictType());
        queryWrapper.eq(StrUtil.isNotBlank(dictTypeQueryParam.getStatus()), "status", dictTypeQueryParam.getStatus());
        if (dictTypeQueryParam.getCreatedTimeStart() != null && dictTypeQueryParam.getCreatedTimeEnd() != null) {
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') >= date_format('" + convert.format(dictTypeQueryParam.getCreatedTimeStart()) + "','%Y-%m-%d')");
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') <= date_format('" + convert.format(dictTypeQueryParam.getCreatedTimeEnd()) + "','%Y-%m-%d')");
        }
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Map<String, Object>> getAll() {
        LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(DictType::getId, DictType::getDictType);
        return this.listMaps(queryWrapper);
    }
}
